﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.WS.UserControls;
using GCELibrary.Current;
using System.Transactions;
using ZF.BLL;
using Newtonsoft.Json;
using ZF.BLL.Admin;
namespace ZF.WS.Admin
{
 
    public partial class msgManage : _msg
    {
    

        [PageArgument(Key = "id", CanNull = false, BindOperType = PageOperType.Edit)]
        public int MsgId { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                //if (this.OperType == PageOperType.AddNew)
                //    Add();
                //else if (this.OperType == PageOperType.Edit)
                    Edit();
            }
        }
        public void InitControls()
        {

            //this.ddlParams.DataSource = DictionaryConfig.MTypeJsonList.Select(p => new
            //{
            //    Key = p.MType.MerchandiseTypeId,
            //    Text = p.MType.Name,
            //});
            //this.ddlParams.DataBind();
            //this.lvMTypeParams.DataSource = DictionaryConfig.MTypeJsonList;
            //this.lvMTypeParams.DataBind();
        }

        //public void Add()
        //{
        //    InitControls();

        //}
        //public void AddSubmit(object sender, EventArgs e)
        //{


        //    using (var dc = SiteInfo.CreateDBDataContext())
        //    {


        //        var art = new ZF.Model.E_Article()
        //        {
        //            AddTime = DateTime.Now,
        //            Cotent = this.fckInfo.Value,
        //            Tag = this.tbTag.Text,
        //            Title = this.tbTitle.Text,
        //            MemberId = CurrentUser.MemberId,
        //        };
        //        try
        //        {
        //            SiteInfo.CheckFilterWords(art);

        //            dc.E_Article.InsertOnSubmit(art);
        //            dc.SubmitChanges();
               
        //        }
        //        catch (Exception ex)
        //        {
        //            throw WarnFactory.CreateWarn(ex);
        //        }
        //        string msgKey = CTitle;

        //        SiteInfo.InsertLog("添加" + msgKey, null);
        //        WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一篇{0}，点击确定按钮返回上一页", msgKey));
        //        link.AddWarnMessageLink(string.Format("继续发布{0}", msgKey), Request.Url.ToString());
        //        WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage).Alert();

        //    }

        //}

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                var msg = dc.E_Msg.FirstOrDefault(p => p.RMemberId == CurrentUser.MemberId && p.MsgId == this.MsgId);
                if (msg == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                InitControls();
                this.tbRContent.Text = msg.RContent;
                this.tbCType.Text = msg.CType;
                this.tbPContent.Text = msg.PContent;
                this.tbContact.Text = msg.Contact;
            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var msg = dc.E_Msg.FirstOrDefault(p => p.RMemberId == CurrentUser.MemberId && p.MsgId == this.MsgId);
                    if (msg == null)
                        throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);

                    msg.RContent = this.tbContact.Text;
                 
                    try
                    {
                        SiteInfo.CheckFilterWords(msg);
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    string msgKey = CTitle;


                    WarnFactory.CreateWarn(new PageWarnParameter()
                    {
                        JscriptMode = true,
                        Link = WarnLink.GoBack,
                        Message = "操作成功",
                    }).Alert();

                    SiteInfo.InsertLog("回复" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功回复{0}，点击确定按钮返回{0}列表", msgKey));

                    link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey), Request.Url.ToString());
                    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage).Alert();

                }


            }

        }

    
    }
 
}
